<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;

/**
 * 佣金记录管理
 *
 * @icon fa fa-circle-o
 */
class BrokerageLog extends Backend
{

    /**
     * BrokerageLog模型对象
     * @var \app\admin\model\user\BrokerageLog
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\user\BrokerageLog;
        $this->view->assign("moldList", $this->model->getMoldList());
        $this->view->assign("mold2List", $this->model->getMold2List());
    }






    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['fromusergroup','fromuser','tousergroup','touser','claimshop','usercouponexchange','orderid'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
                $row->order_jump_link = url('order/order/detail',['ids' => $row->order_id]);
            }

            $total_amount = $this->model->where($where)->sum('amount');
            $total_brokerage = $this->model->where($where)->sum('brokerage');

            $result = array("total" => $list->total(), "rows" => $list->items(), 'total_amount' => $total_amount, 'total_brokerage' => $total_brokerage);

            return json($result);
        }
        return $this->view->fetch();
    }


}
